import grpc
import time
import pic_pb2, pic_pb2_grpc
import cv2
import base64
import numpy as np

_HOST = 'localhost'
_PORT = '8080'

imgPath="/home/guilin/protobuff-test/translateImg/test_img/ts91.png"

# class FormatData(data_pb2_grpc.FormatDataServicer):
#     # 重写接口函数.输入和输出都是proto中定义的Data类型
#     def DoFormat(self, request, context):
#         str = request.text
#         return data_pb2.actionresponse(text=str.upper())  # 返回一个类实例

def run():
    conn = grpc.insecure_channel(_HOST + ':' + _PORT)  # 监听频道
    print(conn)
    client = pic_pb2_grpc.FormatDataStub(channel=conn)  # 客户端使用Stub类发送请求,参数为频道,为了绑定链接

    print(client)
    #cap = cv2.VideoCapture(0)
    while True:







        f=open(imgPath,'rb').read()
        timeStart = time.time()
        print("startTime", timeStart)


        response = client.DoFormat(pic_pb2.actionrequest(text=f))  # 返回的结果就是proto中定义的类

        recieveTime=time.time()
        costTime=response.text-timeStart
        print( "server recieve time" ,response.text)
        print("client to server : ",costTime)
        print("client to server to client: ", recieveTime-timeStart)
        break


if __name__ == '__main__':
    run()